package com.store.pet.petstore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.adapter.CommodityAdapter;
import com.store.pet.petstore.adapter.PetAdapter;
import com.store.pet.petstore.bean.CommodityBean;
import com.store.pet.petstore.bean.PetListBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 宠物列表页面
 */
public class PetListActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_pet)
    RecyclerView recyclerPet;

    private List<PetListBean.ContentBean.PetlistBean> list = new ArrayList<>();

    private PetAdapter adapter;

    private int type = 0;  //宠物类型

    public static final int BUY_SUCCESS = 0x0004;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_pet_list;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出

        type = getIntent().getIntExtra("type",0);
        switch (type){
            case 1:
                title.setText("在售狗狗");
                break;
            case 2:
                title.setText("在售猫猫");
                break;
            case 3:
                title.setText("在售小宠");
                break;
        }

        recyclerPet.setLayoutManager(new LinearLayoutManager(this));
        recyclerPet.addItemDecoration(new SpaceItemDecoration(30));
        adapter = new PetAdapter(R.layout.item_pet_rv,list);
        recyclerPet.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.pet_item:
                        Intent intent = new Intent(PetListActivity.this,PetDetailActivity.class);
                        intent.putExtra("pet",list.get(position));
                        startActivityForResult(intent,BUY_SUCCESS);
                        break;
                }
            }
        });
    }

    @Override
    public void loadData() {
        list.clear();
        if(type != 0){
            Map<String,Object> map = new HashMap<>();
            map.put("type",type);
            OKHttpUtils.requestData(Constant.QUERY_ONSALE_PET_LIST, map, new OKHttpUtils.ServerCallBack() {
                @Override
                public void onSuccess(String data) {
                    PetListBean bean = GsonUtils.parsePetListBean(data);
                    list.addAll(bean.getContent().getPetlist());
                    new Thread(() -> runOnUiThread(() -> adapter.notifyDataSetChanged())).start();  //在主线程刷新界面UI
                }

                @Override
                public void onFailed(String message) {
                    ToastUtils.showShort("获取数据失败");
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data != null){
            switch (requestCode){
                case BUY_SUCCESS:
                    loadData();
                    break;
            }
        }
    }

}
